<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
<!--        <blockquote class="layui-elem-quote">-->
<!--            Layui的树形表格treeTable，支持异步加载(懒加载)、复选框联动、折叠状态记忆。<br>-->
<!--            <a href="https://gitee.com/whvse/treetable-lay" target="_blank" class="layui-btn layui-btn-danger">treetable-lay</a>-->
<!--        </blockquote>-->
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn"  id="btn-add"> 添加类别 </button>
            </div>



            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>


</div>

<!-- 操作列 -->
<script type="text/html" id="auth-state">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">启用</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}"></script>
<script>
    layui.use(['table', 'treetable', 'form'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '/commodity/categoryInfo',
            page: false,
            cols: [
                [
                {type: 'numbers'},
                // {field: 'id', minWidth: 50, title: '类别id'},
                {field: 'name', minWidth: 150, title: '类别名称'},
                // {field: 'role', title: '权限标识'},
                // {field: 'href', title: '菜单路径'},
                {field: 'remark', width: 1024, title: '备注'},

                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $('#btn-add').click(function () {
            var index = layer.open({
                title: '添加类别',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/commodity/getAddCategoryPage',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });


        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                $.ajax({
                    type: "post",
                    url: "/commodity/delCategoryById?id=" + data.id,
                    beforeSend: function() {
                        layer.msg("正在处理...");
                    },
                    success: function (data) {
                        if (data.code == 500) {
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message);
                            // layer.msg('已禁用' +'  ' + obj.data.title);
                            function send() {

                                window.location.reload();
                            }
                            setTimeout(send,1500);

                        }
                    },
                    error: function (data) {
                        layer.msg("禁用失败！");
                    }
                })

            } else if (layEvent === 'edit') {
                $.ajax({
                    type: "post",
                    url: "/commodity/openCategoryById?id=" + data.id,
                    beforeSend: function() {
                        layer.msg("正在处理...");
                    },
                    success: function (data) {
                        if (data.code == 500) {
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message);
                            // layer.msg('已启用' +'  ' + obj.data.title);
                            function send() {
                                // window.parent.location.reload();
                                window.location.reload();
                            }
                            setTimeout(send,1500);

                        }
                    },
                    error: function (data) {
                        layer.msg("启用失败！");
                    }
                })
            }
        });
    });
</script>
</body>
</html>